package com.ruoyi.creditlife.service.impl;

import com.ruoyi.creditlife.domain.BookBorrrowRecord;
import com.ruoyi.creditlife.domain.BookRenewRecords;
import com.ruoyi.creditlife.mapper.BookBorrowTrackingMapper;
import com.ruoyi.creditlife.mapper.BookBorrrowRecordMapper;
import com.ruoyi.creditlife.mapper.BookRenewRecordsMapper;
import com.ruoyi.creditlife.mapper.CreditLifeBehaviorMapper;
import com.ruoyi.creditlife.service.IBookRenewRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 续借记录Service业务层处理
 * 
 * @author ddd
 * @date 2025-07-10
 */
@Service
public class BookRenewRecordsServiceImpl implements IBookRenewRecordsService 
{
    @Autowired
    private BookRenewRecordsMapper bookRenewRecordsMapper;
    @Autowired
    private BookBorrrowRecordMapper bookBorrrowRecordMapper;
    @Autowired
    private CreditLifeBehaviorMapper creditLifeBehaviorMapper;
    @Autowired
    private BookBorrowTrackingMapper bookBorrowTrackingMapper;
    /**
     * 查询续借记录
     * 
     * @param renewId 续借记录主键
     * @return 续借记录
     */
    @Override
    public BookRenewRecords selectBookRenewRecordsByRenewId(Long renewId)
    {
        return bookRenewRecordsMapper.selectBookRenewRecordsByRenewId(renewId);
    }

    /**
     * 查询续借记录列表
     * 
     * @param bookRenewRecords 续借记录
     * @return 续借记录
     */
    @Override
    public List<BookRenewRecords> selectBookRenewRecordsList(BookRenewRecords bookRenewRecords)
    {
        return bookRenewRecordsMapper.selectBookRenewRecordsList(bookRenewRecords);
    }

    /**
     * 新增续借记录
     * 
     * @param bookRenewRecords 续借记录
     * @return 结果
     */
    @Override
    public int insertBookRenewRecords(BookRenewRecords bookRenewRecords)
    {
        return bookRenewRecordsMapper.insertBookRenewRecords(bookRenewRecords);
    }

    /**
     * 修改续借记录
     * 
     * @param bookRenewRecords 续借记录
     * @return 结果
     */
    @Override
    public int updateBookRenewRecords(BookRenewRecords bookRenewRecords)
    {
        return bookRenewRecordsMapper.updateBookRenewRecords(bookRenewRecords);
    }

    /**
     * 批量删除续借记录
     * 
     * @param renewIds 需要删除的续借记录主键
     * @return 结果
     */
    @Override
    public int deleteBookRenewRecordsByRenewIds(Long[] renewIds)
    {
        return bookRenewRecordsMapper.deleteBookRenewRecordsByRenewIds(renewIds);
    }

    /**
     * 删除续借记录信息
     * 
     * @param renewId 续借记录主键
     * @return 结果
     */
    @Override
    public int deleteBookRenewRecordsByRenewId(Long renewId)
    {
        return bookRenewRecordsMapper.deleteBookRenewRecordsByRenewId(renewId);
    }
    @Override
    public int ReNewBook(BookBorrrowRecord borrrowRecord,Long extendDays)
    {
        BookRenewRecords bookRenewRecord =bookRenewRecordsMapper.selectBookRenewRecordsByBorrowId(borrrowRecord.getId());
        if(bookRenewRecord!=null){
            if(bookRenewRecord.getRenewCount()>3||extendDays>7){
                return -1;
            }
            bookRenewRecord.setRenewCount(bookRenewRecord.getRenewCount()+1);
            bookRenewRecord.setRenewTime(new Date());
            bookRenewRecord.setExtendDays(extendDays);
            LocalDate oldDueDate = borrrowRecord.getDueTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate newLocalDueDate = oldDueDate.plusDays(extendDays);
            Date newDueDate = Date.from(
                    newLocalDueDate.atStartOfDay(ZoneId.systemDefault())
                            .toInstant()
            );
            bookRenewRecord.setNewDueTime(newDueDate);
            borrrowRecord.setDueTime(newDueDate);
            bookBorrrowRecordMapper.updateBookBorrrowRecord(borrrowRecord);
            return  bookRenewRecordsMapper.updateBookRenewRecords(bookRenewRecord);
        }
        else {
            BookRenewRecords bookRenewRecords = new BookRenewRecords();
            bookRenewRecords.setBorrowId(borrrowRecord.getId());
            bookRenewRecords.setUserId(borrrowRecord.getUserId());
            bookRenewRecords.setExtendDays(extendDays);
            LocalDate oldDueDate = borrrowRecord.getDueTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate newLocalDueDate = oldDueDate.plusDays(extendDays);
            Date newDueDate = Date.from(
                    newLocalDueDate.atStartOfDay(ZoneId.systemDefault())
                            .toInstant()
            );
            bookRenewRecords.setNewDueTime(newDueDate);
            bookRenewRecords.setRenewTime(new Date());
            bookRenewRecords.setRenewCount((long)1);
            borrrowRecord.setDueTime(newDueDate);
            bookBorrrowRecordMapper.updateBookBorrrowRecord(borrrowRecord);
           return bookRenewRecordsMapper.insertBookRenewRecords(bookRenewRecords);
        }
    }
}
